<?php
/**
 * Created by PhpStorm.
 * User: 47557
 * Date: 2017/4/13
 * Time: 14:37
 */

namespace app\db\controller;


use app\db\model\RemenberInf;
use think\Db;
use think\Exception;

class Remenbercontrol extends Dbcommoncontrol
{
    protected $DbName = 'RemenberInf';

    function GetRemmenberinf()
    {
        try {
            $u_id = $_POST["u_id"];
            $p_id = $_POST["p_id"];
            $time = "";
            try {
                $time = $_POST["time"];
            } catch (Exception $ex) {
            }
            $recordlist = Db::table($this->DbName)->where('U_Id', '=', $u_id)
                ->where('P_Id', '=', $p_id);
            if ($time != "") {
                $recordlist = $recordlist->where('R_Time', '=', $time)->select();
            } else
                $recordlist = $recordlist->select();
            if ($recordlist != null) {
                //var_dump($recordlist);
                return json_encode($recordlist);
            }
        } catch (Exception $ex) {
            return $ex->getMessage();
        }
    }
    /**
     * 获取今天的背单词信息
     * @return bool|\Exception|string|Exception
     */
    function GetTodayinf()
    {
        $u_id = $_POST["u_id"];
        $p_id = $_POST["p_id"];
        $record = DB::table($this->DbName)
            ->where('R_Time', '=', date("Y-m-d"))
            ->where('U_id', '=', $u_id)
            ->where('P_id', '=', $p_id)
            ->find();
        //没有记录证明这是今天第一次读取记单词信息    汇总更新昨天plan的数据 并且创建今天的记录
        if ($record == null) {
            if ($this->MakePlan($u_id, $p_id)) {
                //在记录表给他一个初始值
                $data = ['R_Time' => date("Y-m-d"), 'R_RCount' => 0, 'R_RWord' => "", 'R_EWord' => '', 'U_Id' => $u_id, 'P_Id' => $p_id];
                Db::table($this->DbName)->insert($data);
                return "start";
            } else
                return "false";
        } else
            return json($record);
    }
    /**
     * 利用记录信息表中的信息汇总更新计划表中的数据
     *
     * @param $u_id用户id
     * @param $p_id计划id
     * @return int|string
     */
    function MakePlan($u_id, $p_id)
    {
        //找到和这个计划相关都所有记录信息
        $listrecord = DB::table($this->DbName)
            ->where('U_Id', '=', $u_id)
            ->where('P_Id', '=', $p_id)
            ->select();
        $R_RWord = "";
        $R_EWord = "";
        //有相关的记录信息汇总填写到计划信息
        try {
            foreach ($listrecord as $key => $value) {
                $R_RWord .= "," . $value['R_RWord'];
                $R_EWord .= "," . $value['R_EWord'];
            }
            $R_RWord = explode(",", $R_RWord);
            $R_RWord = array_filter(array_unique($R_RWord));
            $R_EWord = explode(",", $R_EWord);
            $R_EWord = array_filter(array_unique($R_EWord));
            $record = DB::table('PlanInf')
                ->where('U_Id', '=', $u_id)
                ->where('P_Id', '=', $p_id)
                ->find();
            $record['P_RemenberWord'] = implode(',', $R_RWord);
            $record['P_ReviewList'] = implode(',', $R_EWord);
            Db::table('PlanInf')
                ->update($record);
            return true;
        } catch (Exception $ex) {
            return false;
        }
    }
    /***
     * @return int|string
     * 用于背单词时更新今天的记忆信息
     *
     */
    function Updateinf()
    {
        $u_id = $_POST["u_id"];
        $p_id = $_POST["p_id"];
        $data = Db::table($this->DbName)
            ->where('R_Time', '=', date("Y-m-d"))
            ->where('U_id', '=', $u_id)
            ->where('P_id', '=', $p_id)
            ->find();
        if($data==null)
            return "error";
        if (isset($_POST["RWord"])) {
            $R_Word = $_POST["RWord"];
            $data['R_RWord'] .= $data['R_RWord'] != '' ? (',' . $R_Word) : $R_Word;
            $data['R_RWord'] = implode(',', array_unique(explode(',', $data['R_RWord'])));
            if (strstr($data['R_EWord'], ',' . $R_Word))
                $data['R_EWord'] = str_replace(',' . $R_Word, '', $data['R_EWord']);
            else if (strstr($data['R_EWord'], $R_Word . ',')) {
                $data['R_EWord'] = str_replace($R_Word . ',', '', $data['R_EWord']);
            }
            else
                $data['R_EWord'] = str_replace($R_Word, '', $data['R_EWord']);
        }
        if (isset($_POST["EWord"])) {
            $E_Word = $_POST["EWord"];
            $data['R_EWord'] .= $data['R_EWord'] != '' ? (',' . $E_Word) : $E_Word;
            $data['R_EWord'] = implode(',', array_unique(explode(',', $data['R_EWord'])));
        }
        $this->MakePlan($u_id, $p_id);
        return Db::table($this->DbName)->update(array_unique($data));
    }
}